      SUBROUTINE RDCAT(LUCATLG,
     *   NTARGS,KTARGID,TARGNAMES,KTARGTYP,MAXPARM,TARGPARM,
     *   NFOUND,LUPRINT,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE RETRIEVES TARGET INFORMATION FROM THE TARGET CATALOGUE.
C THE KIND OF INFORMATION RETRIEVED IS DESCRIBED BELOW UNDER VARIABLE
C NAMES TARGNAMES, KTARGTYP, AND TARGPARM. THE CALLER SPECIFIES THE
C TARGETS OF INTEREST BY GIVING THE TARGET ID NUMBERS AS THEY APPEAR IN
C THE CATALOGUE BEING READ, OR BY INDICATING THAT ALL CATALOGUE ELEMENTS
C ARE WANTED.
C
C WHEN THE READER ENCOUNTERS A CATALOGUE RECORD WITH ZERO OR NEGATIVE
C TARGET ID AND/OR ZERO OR NEGATIVE TARGET TYPE, IT TREATS THE RECORD
C AS NOT EXISTING. IT IMMEDIATELY READS THE NEXT RECORD IN THE
C CATALOGUE.
C
C
C VARIABLE  DIM  TYPE I/O DESCRIPTION
C --------  ---  ---- --- -----------
C
C LUCATLG    1    I*4  I  THE FORTRAN UNIT NUMBER OF THE TARGET
C                         CATALOGUE BEING READ.
C
C NTARGS     1    I*4  I  THIS IS THE NUMBER OF TARGETS FOR WHICH
C                         CATALOGUE INFORMATION IS WANTED.
C
C                         NTARGS IS USED TO DIMENSION KTARGID, TARGNAME,
C                         AND TARGPARM. THE CALLER MUST BE SURE THAT ITS
C                         ACTUAL ARRAY DIMENSIONS ARE THE SAME OR LARGER
C                         THAN NTARGS. OTHERWISE THIS ROUTINE WILL STORE
C                         DATA OUTSIDE OF THE ARRAY BOUNDARIES.
C
C                         IF USING KTARGID(1) = 'ALL'(SEE BELOW), THEN
C                         NTARGS MUST BE LARGE ENOUGH TO HOLD THE
C                         ENTIRE CATALOGUE.
C
C KTARGID  NTARGS I*4     THE USAGE DEPENDS ON KTARGID(1).
C
C                      I  IF KTARGID(1).NE.'ALL' --
C
C                         KTARGID CONTAINS THE TARGET ID'S FOR WHICH
C                         CATALOGUE DATA IS WANTED. IF SOME ARE NOT
C                         FOUND, IERR IS SET.
C
C                         FOR ZERO OR NEGATIVE KTARGID'S, NO CATALOGUE
C                         SEARCH IS DONE, BUT TARGNAMES, KTARGTYP, AND
C                         TARGPARM ARE LOADED AS DESCRIBED BELOW.
C
C                         IN ERROR RETURN SITUATION: NO CHANGE FROM
C                         CONTENTS AT ENTRY.
C
C                     I/O IF KTARGID(1).EQ.'ALL' --
C
C                         THE CALLER IS TELLING THIS ROUTINE THAT TARGET
C                         ID'S ARE NOT INDIVIDUALLY SPECIFIED AND THAT
C                         INFO FOR ALL TARGETS ON THE FILE SHOULD BE
C                         RETURNED.
C
C                         THE KTARGID ARRAY IS FILLED WITH THE TARGET
C                         ID'S FROM THE FILE. IF THERE ARE FEWER THAN
C                         NTARGS TARGETS, THE BALANCE OF THE ARRAY IS
C                         FILLED WITH 0'S.
C
C                         IN ERROR RETURN SITUATION:
C
C                           NTARGS TOO SMALL:  ALL KTARGID'S = 0
C                           OTHER ERRORS       = ID'S FROM THE FILE
C
C                         USING 'ALL' ON A CALL FROM A SIMPLE MAIN IS
C                         AN EASY WAY TO VERIFY THAT AN ENTIRE CATALOGUE
C                         IS OK BEFORE USING IT.
C
C TARGNAMES      CH*16 O  TARGNAMES(I) IS THE TARGET NAME ASSOCIATED
C          NTARGS         WITH KTARGID(I) AS FOUND IN THE CATALOGUE.
C
C                         FOR NEGATIVE OR ZERO KTARGID(I), TARGNAMES(I)
C                         IS LOADED WITH 'NOT WANTED'.
C
C                         IN ERROR RETURN SITUATION: IF AN ERROR IS
C                         SENSED FOR KTARGID(I), THEN TARGNAMES(I)
C                         IS LOADED ACCORDING TO THE ERROR.
C
C                          TARGET NOT FOUND:   = 'NOT IN CATALOGUE'
C                          DATA MISSING:       = 'RDCAT ERROR 2'
C                          MAXPARMS TOO SMALL: = 'RDCAT ERROR 4'
C                          NTARGS TOO SMALL:   = 'RDCAT ERROR 8'
C                          MISC ERROR:         = 'RDCAT ERROR 4096'
C
C KTARGTYP        I*4  O  KTARGTYP(I) IS THE TARGET TYPE ASSOCIATED
C          NTARGS         WITH KTARGID(I) AS FOUND IN THE CATALOGUE.
C
C                         TARGET TYPE ASSIGNMENTS ARE FOUND IN THE
C                         CATALOGUE FORMAT DOCUMENTATION.
C
C                         FOR NEGATIVE OR ZERO KTARGID(I),
C                         KTARGTYP(I) = 0
C
C                         IN ERROR RETURN SITUATION: IF AN ERROR IS
C                         SENSED FOR KTARGID(I), THEN KTARGTYP(I)
C                         IS LOADED ACCORDING TO THE ERROR.
C
C                            TARGET NOT FOUND:   = -1
C                            DATA MISSING:       = -2
C                            MAXPARMS TOO SMALL: = -4
C                            NTARGS TOO SMALL:   = -8
C                            MISC ERROR:         = -4096
C
C MAXPARM    1    I*4  I  THE NUMBER OF PARAMETERS THE TARGPARM ARRAY,
C                         DESCRIBED BELOW, CAN HOLD FOR EACH TARGET.
C                         MUST BE 1 OR GREATER.
C
C                         MAXPARM IS THE FIRST DIMENSION OF THE TARGPARM
C                         ARRAY AND MUST BE THE SAME VALUE USED IN THE
C                         CALLING ROUTINE TO DIMENSION TARGPARM.
C
C                         THE NUMBER NEEDED DEPENDS ON THE TARGET TYPES
C                         INVOLVED. IF YOU KNOW THE TYPES OF TARGETS,
C                         YOU CAN DETERMINE THE SMALLEST VALID MAXPARM
C                         VALUE FROM THE TARGET CATALOGUE FORMAT
C                         DESCRIPTION DOCUMENT. IF YOU DON'T KNOW, USE
C                         THE MAXDATA VALUE FROM RDCAT.INC
C
C                         EXAMPLE: IF YOUR APPLICATION USES TARGET TYPES
C                         NEEDING AT MOST 3 PIECES OF INFORMATION,
C                         THEN MAXPARM MUST BE 3 OR GREATER.
C
C                         IF A TARGET IS WANTED THAT NEEDS MORE SPACE
C                         THAN MAXPARM ALLOWS, AN ERROR CONDITION EXISTS
C
C TARGPARM        R*8  O  TARGPARM(-,I) IS THE LOCATION INFORMATION
C      MAXPARM,NTARGS     RETRIEVED FROM THE CATALOGUE FOR THE TARGET
C                         HAVING ID=KTARGID(I). THE QUANTITIES INVOLVED
C                         DEPEND UPON THE TARGET TYPE.
C
C                         THE CATALOGUE FILE FORMAT DESCRIPTION IN THE 
C                         TOSS DOCUMENTATION USES AN ARRAY, TARGDATA,
C                         TO DEFINE TARGET INFORMATION. TARGPARM(K,I)
C                         HAS THE SAME DESCRIPTION AS TARGDATA(K,I).
C
C                         OUTPUT UNITS: ANGLE QUANTITIES ARE IN RADIANS
C                         AND EPOCH TIME FOR TARGET TYPE 7 IS IN SECONDS
C                         SINCE 1/1/50, 0.0 HRS. 
C
C                         IF SOME TARGET TYPES INVOLVED NEED FEWER THAN
C                         MAXPARMS ELEMENTS, THEN SOME OF THE ELEMENTS
C                         ARE NOT LOADED USING TARGET CATALOGUE DATA.
C                         THOSE NOT LOADED FROM THE FILE CONTAIN THE
C                         VALUE 8888.D0
C
C                         ALL TARGPARM ELEMENTS ARE FILLED WITH THE
C                         VALUE 8888.D0, THEN OVERRIDDEN INDIVIDUALLY
C                         WHEN A MATCH BETWEEN KTARGID AND THE CATALOGUE
C                         OCCURS. THEREFORE, FOR ZERO OR NEGATIVE
C                         KTARGID'S,TARGPARM(-ALL-,I)=8888.D0
C
C                         IN ERROR RETURN SITUATION: IF AN ERROR IS
C                         SENSED FOR KTARGID(I), THEN TARGPARM(*,I)
C                         IS FILLED WITH ZEROS.
C
C NFOUND     1    I*4  O  THE NUMBER OF TARGETS FOR WHICH MATCHES WERE
C                         FOUND.
C
C                         NFOUND WILL BE DIFFERENT THAN NTARGS IF NTARGS
C                         IS NEGATIVE OR SOME KTARGID'S ARE ZERO OR
C                         NEGATIVE.
C
C                         IN ERROR RETURN SITUATION:
C
C                           IF KTARGID(1)='ALL' WAS USED AND NTARGS IS
C                           TOO SMALL, NFOUND = MINUS THE VALUE OF
C                           NTARGS THAT WOULD HAVE WORKED. FOR EXAMPLE,
C                           IF NFOUND = -150, THEN NTARGS = 150 OR
C                           LARGER IS NEEDED.
C
C                           FOR OTHER ERRORS, NO EFFECT.
C
C LUPRINT    1    I*4  I  THE FORTRAN UNIT NUMBER WHERE TARGET CATALOGUE
C                         INFORMATION IS TO BE PRINTED. IF ZERO OR
C                         NEGATIVE, NO PRINT OCCURS.
C
C LUERR      1    I*4  I  THE FORTRAN UNIT NUMBER WHERE ERROR MESSAGES
C                         ARE TO BE WRITTEN. IF ZERO OR NEGATIVE, NO
C                         MESSAGES ARE GIVEN.
C
C IERR       1    I*4  O  ERROR RETURN FLAG. THIS OCCURS WHEN :
C
C                          = 0  NO ERROR.
C                          = OTHERWISE, IS THE SUM OF POSSIBLE ERROR
C                            CONDITIONS:
C
C                             1  A CALLING PROGRAM ERROR HAS OCCURRED.
C                                ONE OR MORE TARGET ID'S WERE NOT FOUND
C                                IN THE CATALOGUE.
C
C                             2  A CATALOGUE CONTENT ERROR HAS OCCURRED.
C                                DATA FOR ONE OR MORE REQUESTED TARGETS
C                                IS ABSENT.
C
C                             4  A CALLING PROGRAM ERROR HAS OCCURRED.
C                                CATALOGUE DATA FOR ONE OR MORE TARGETS
C                                NEEDS MORE THAN MAXPARMS ARRAY ELEMENTS
C                                IN TARGPARM.
C
C                             8  A CALLING PROGRAM ERROR HAS OCCURRED.
C                                KTARGID(1)='ALL' WAS INPUT BUT NTARGS
C                                WAS TOO SMALL TO HOLD THE CATALOGUE
C                                DATA.
C
C                           4096 A MISCELLANEOUS ERROR NOT COVERED
C                                ABOVE.
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742, 12/84, 3/85, 7/85
C    MODIFIED.....
C
C***********************************************************************
C
      INCLUDE 'RDCAT.INC'  ! SUPPLIES MAXDATA PARAMETER VALUE
C
      PARAMETER MAXDATA1 = MAXDATA+1
      REAL*8    TARGDATA(MAXDATA1)
      REAL*8    TARGPARM(MAXPARM,1) ! ACTUAL= TARGPARM(MAXPARM,NTARGS)
      INTEGER*4 KTARGID(1)          ! ACTUAL= KTARGID(NTARGS)
      INTEGER*4 KTARGTYP(1)         ! ACTUAL= KTARGTYP(NTARGS)
      CHARACTER*16 TARGNAMES(1)     ! ACTUAL= TARGNAMES(NTARGS)
C
      CHARACTER*16 NAME
      LOGICAL FOUNDIT
      CHARACTER*1 LINEFEED
      INTEGER INIT/1/,LUCATPREV/-99/
      REAL*8 R8888/8888.D0/
      REAL*8 DEGRAD / 57.29577951308232D0 /
C
      IBUG = 0
      LUBUG = 19
C
      IF(IBUG.NE.0) WRITE(LUBUG,7501)
     *    NTARGS,MAXPARM,(KTARGID(I),I=1,NTARGS)
 7501 FORMAT(' RDCAT DEBUG. NTARGS=',I3,'  MAXPARM=',I3, '   KTARGID='/,
     *          <(NTARGS+9)/10>(T5,10I6/))
C
C
C **********************************
C *  INITIALIZATION FOR THIS CALL  *
C **********************************
C
      IERR1 = 0
      IERR2 = 0
      IERR4 = 0
      IERR8 = 0
      IERRX = 0
      NFOUND = 0
C
      IF(NTARGS.LE.0) THEN
        IF(LUPRINT.GT.0) WRITE(LUPRINT,1060) NTARGS
        GO TO 9999
        END IF
C
C
C ON THE FIRST CALL, CHECK FOR MAJOR CATALOGUE CONTENT ERRORS.
C
      IF(LUCATLG.NE.LUCATPREV) THEN
        CALL RDCAT1(LUCATLG,LUERR,IER1)
        IF(IER1.NE.0) THEN
          IF(LUERR.GT.0) WRITE(LUERR,1050) 
          STOP 'CATALOGUE CONTENT ERRORS. STOP 1 IN RDCAT.'
          END IF
        END IF
C
C IF KTARGID(1) = 'ALL', LOAD KTARGID ARRAY FROM THE CATALOGUE, THEN
C PROCEED AS THOUGH ID'S HAD COME FROM THE CALLER. NTARGS MUST BE LARGE
C ENOUGH TO HOLD THE CATALOGUE. IF NOT LARGE ENOUGH, ERROR RETURN
C OCCURS FROM RDCAT2.
C
      IF(KTARGID(1).EQ.'ALL') THEN
        CALL RDCAT2(LUCATLG,
     *     NTARGS,KTARGID,TARGNAMES,KTARGTYP,MAXPARM,TARGPARM,
     *     NFOUND,LUERR,IER1)
        IF(IER1.NE.0) THEN
          IERR8 = 8
          GO TO 9900
          END IF
        END IF
C
C INITIALIZE TARGPARM, KTARGTYP, AND TARGNAMES
C
      CALL MTXSETR8(TARGPARM,R8888,MAXPARM,NTARGS)
      DO ITARG=1,NTARGS
        IF(KTARGID(ITARG).GT.0) THEN
          TARGNAMES(ITARG) = 'NOT IN CATALOGUE'
          KTARGTYP(ITARG) = -1
        ELSE
          TARGNAMES(ITARG) = 'NOT WANTED'
          KTARGTYP(ITARG) =  0
          END IF
        END DO
C
C
C *********************************
C *  GET INFO FROM THE CATALOGUE  *
C *********************************

      CALL RDCAT3(LUCATLG,
     *   NTARGS,KTARGID,TARGNAMES,KTARGTYP,MAXPARM,TARGPARM,
     *   NFOUND,LUERR,IERR2,IERR4,IERRX)
C
C
C *************
C *  WRAP UP  *
C *************
C
 9900 CONTINUE
C
C CHECK FOR TARGET ID'S NOT FOUND.
C
      LINEFEED = '0'
      DO ITARG=1,NTARGS
        IF(KTARGID(ITARG).GT.0) THEN
          FOUNDIT = KTARGTYP(ITARG).GT.0
          IF(.NOT.FOUNDIT) THEN
            IERR1 = 1
            CALL MTXSETR8(TARGPARM(1,ITARG),0.D0,MAXPARM,1)
            IF(KTARGTYP(ITARG).EQ.-1) THEN
              IF(LUERR.GT.0) WRITE(LUERR,911) LINEFEED,KTARGID(ITARG)
            ELSE IF(KTARGTYP(ITARG).LE.-2) THEN
              IF(LUERR.GT.0) WRITE(LUERR,912) LINEFEED,KTARGID(ITARG),
     *                      KTARGTYP(ITARG)
            ELSE    ! SHOULD NEVER ID POSITIVE AND TYPE = ZERO.
              TYPE *,' RDCAT. PROGRAMMER ERROR. SEE CODE.'
              STOP   ' RDCAT. PROGRAMMER ERROR. STOP 2. SEE CODE.'
              END IF
            LINEFEED = ' '
            END IF
          END IF
        END DO
C
C PRINT INFO BEING RETURNED
C
      CALL RDCAT4(LUCATLG,
     *    NTARGS,KTARGID,TARGNAMES,KTARGTYP,MAXPARM,TARGPARM,
     *    LUPRINT)
C
C
 9999 CONTINUE
      IERR = IERR1 + IERR2 + IERR4 + IERR8 + IERRX
      LUCATPREV = LUCATLG
      RETURN
C
 1050 FORMAT(/,' CATALOGUE CONTENT ERRORS. STOPPED IN RDCAT.'/)
 1060 FORMAT(/,' RDCAT. NO TARGETS ARE WANTED. NTARGS=',I7/)
  911 FORMAT(A,'RDCAT. TARGET ID=',I7,
     *  '  TARGET NOT FOUND IN THE CATALOGUE.')
  912 FORMAT(A,'RDCAT. TARGET ID=',I7,
     *  '  TARGET FOUND IN CATALOGUE. ERROR CONDITION=',I6)
      END
